package sicnu.cs.ich.activity.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.pkcs.jcajce.JcaPKCS8EncryptedPrivateKeyInfoBuilder;
import org.springframework.web.bind.annotation.*;
import sicnu.cs.ich.activity.service.ICommentService;
import sicnu.cs.ich.api.common.ResultInfo;
import sicnu.cs.ich.api.domain.entity.ActivityComments;
import sicnu.cs.ich.common.util.ResultInfoUtil;
import sicnu.cs.ich.security.util.SecurityUtil;

import javax.validation.Valid;

/**
 * @author 黎琦
 * @date 2021/12/11 12:17
 **/
@RestController
@RequestMapping("/comment")
@RequiredArgsConstructor
@Slf4j
@Api(tags = {"评论相关操作"})
public class CommentResource {
    private final ICommentService commentService;

    /**
     * 添加评论
     *
     * @param activityComments
     * @return 评论id
     */
    @PostMapping("/add")
    @ApiOperation("添加评论")
    public ResultInfo<Integer> add(@Valid ActivityComments activityComments){
//        log.info(activityComments.toString());
        activityComments.setUserId(SecurityUtil.getCurrentUserId());
        Integer cid = commentService.insertComment(activityComments);
        return ResultInfoUtil.buildSuccess(cid);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/del")
    public ResultInfo<Boolean> delete(@RequestParam("cid") Integer cid){
        //参数校验
        if(cid==null){
            return ResultInfoUtil.buildError("评论id为空");
        }
        return ResultInfoUtil.buildSuccess(commentService.deleteComment(cid));
    }
}
